package com.wcity.manage.web.Announcement;

import com.alibaba.fastjson.JSON;
import com.wcity.manage.common.context.Constants;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.common.exception.CustomException;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.AnnouncementMapper;
import com.wcity.manage.dao.model.AnnFind;
import com.wcity.manage.dao.model.Announcement;
import com.wcity.manage.dao.model.Block;
import com.wcity.manage.service.announcement.AnnouncementService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前端公告发布
 * Created by zm on 2016/12/19.
 */
@Controller
public class IAnnouncementAct {

    @Resource private AnnouncementService announcementService;
    @Resource private AnnouncementMapper announcementMapper;

    /**
     * 获取物业公告列表
     * @param pageModel
     * @param blockId
     * @return
     */
    @RequestMapping ("pro_ann_list")
    @ResponseBody
    public Map<String,Object> ProAnnList (PageModel pageModel, String blockId ) {

        if(StringUtil.isBlank( blockId )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        Map<String,Object> map = new HashMap<>();
        AnnFind ann = new AnnFind();
        try{
            Block blockInfo = announcementService.getUserId( blockId );
            if ( blockInfo == null) {
                map.put("retCode", Code.ERROR.getCodeNum());
                map.put("retDesc","用户不存在");
            }else {
                ann.setBlockId( Integer.parseInt(blockId) );
                ann.setTypes("物业公告");
                pageModel.setParams(ann);
                List<Announcement> annList = announcementService.getProAnnList(pageModel);
                map.put("admin",announcementService.getUserInfo (blockInfo.getUserId()).getUserName());
                map.put("annList", annList);
                map.put("retCode", Code.SUCCESS.getCodeNum());
                map.put("retDesc", "请求成功");
            }
        }catch ( Exception e ){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }

    /**
     * 获取社区公告列表
     * @param pageModel
     * @param blockId
     * @return
     */
    @RequestMapping ("block_ann_list")
    @ResponseBody
    public Map<String,Object> blockAnnList (PageModel pageModel, String blockId ) {

        if(StringUtil.isBlank( blockId )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        Map<String,Object> map = new HashMap<>();
        AnnFind ann = new AnnFind();
        try{
            Block blockInfo = announcementService.getUserId( blockId );
            if ( blockInfo == null) {
                map.put("retCode", Code.ERROR.getCodeNum());
                map.put("retDesc","用户不存在");
            }else {
                ann.setBlockId( Integer.parseInt(blockId) );
                ann.setTypes("社区公告");
                pageModel.setParams(ann);
                List<Announcement> annList = announcementService.getProAnnList(pageModel);
                map.put("admin",announcementService.getUserInfo (blockInfo.getUserId()).getUserName());
                map.put("annList", annList);
                map.put("retCode", Code.SUCCESS.getCodeNum());
                map.put("retDesc", "请求成功");
            }
        }catch ( Exception e ){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }

    /**
     * 获取公告详情
     * @param annId
     * @return
     */
    @RequestMapping ("ann_info")
    @ResponseBody
    public Map<String,Object> annInfo (String annId , String blockId) {

        if(StringUtil.isBlank( blockId )&&StringUtil.isBlank( annId )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        Map<String,Object> map = new HashMap<>();
        try {
            Block blockInfo = announcementService.getUserId( blockId );
            if ( blockInfo == null) {
                map.put("retCode", Code.ERROR.getCodeNum());
                map.put("retDesc","用户不存在");
            }else {
                Announcement annInfo = announcementService.getAnnInfo1(annId,blockId);
                System.out.println(JSON.toJSONString(annInfo,true));
                map.put("admin",announcementService.getUserInfo (blockInfo.getUserId()).getUserName());
                map.put("AnnInfo", annInfo);
                map.put("retCode", Code.SUCCESS.getCodeNum());
                map.put("retDesc", "请求成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }

    /**
     * 获取社区和物业最新的公告
     * @param blockId
     * @return
     */
    @RequestMapping ("new_ann")
    @ResponseBody
    public Map<String,Object> newAnn (String blockId){
        if(StringUtil.isBlank( blockId )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        System.out.println(StringUtil.isBlank( blockId ));
        Map<String,Object> map = new HashMap<>();
        try{
            //物业最新公告
            Announcement proNewAnn = announcementMapper.getProNewAnn (blockId);
            //社区最新公告
            Announcement comNewAnn = announcementMapper.getComNewAnn (blockId);

            map.put("proNewAnn",proNewAnn);
            map.put("comNewAnn",comNewAnn);
            map.put("retCode", Code.SUCCESS.getCodeNum());
            map.put("retDesc", "请求成功");
        }catch (Exception e) {
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }
}
